<?php
/**
 * Created by PhpStorm.
 * User: bbee888
 * Email: 876337011@qq.com
 */
namespace app\admin\model;
use think\Db;
use think\Model;
use think\facade\Request;

class Order extends Model
{
    protected $table = 'petct_order';
    public $notes = array(//订单状态数值注释
		'state' => array(1 => '待处理', 2 => '待就诊', 3 => '待确认', 4 => '已就诊有效', 5 => '已就诊无效', 6 => '未就诊', 7 => '已取消'),
        'form' => array(1 => '表单', 2 => '电话', 3 => '微信', 4 => '转介绍'),
	);

    /**
     * 获取订单信息
     * @param $limit
     * @param $where
     * @return array
     */
    public function getOrders($limit, $where)
    {
        $prefix = config('database.prefix');

        try {

            $res = $this->where($where)
                ->where('status',1)
                ->order('id', 'desc')->paginate($limit);

        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }
    /**
     * 根据state获取订单信息
     * @param $limit
     * @param $where
     * @return array
     */
    public function getOrderByState($limit, $where, $state)
    {
        try {
            $res = $this->where($where)
                ->where('state',$state)
                ->where('status',1)
                ->order('id', 'desc')->paginate($limit);

        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }
    /**
     * 获取订单信息
     * @param $limit
     * @param $where
     * @return array
     */
    public function getOrderMoney($limit, $where) {
        try {
            $res = Db::name('order')
                ->alias('a')
                ->leftJoin('order_money b', 'a.id = b.order_id')
                ->field('a.*,sum(b.profit) as profit,sum(b.pay) as pay')
                ->group('a.id')
                ->where($where)
                ->order('a.id desc')
                ->paginate($limit);
        } catch (\Exception $e) {
            return modelReMsg(-1, '', $e->getMessage());
        }
        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 增加订单
     * @param $params
     * @return array
     */
    public function addOrder($params)
    {
        try {

            //当提交的姓名、电话是相同的，就表示用户已存在数据库
            $where = [
                'phone'=>$params['phone'],
                'name'=>$params['name']
            ];
            $flag = $this->where($where)->count();
            if ($flag) {
                return modelReMsg(-2, '', '订单已经存在');
            }

            $params['add_time'] = date('Y-m-d H:i:s');
            $params['update_time'] = date('Y-m-d H:i:s');
            $params['check_time'] = date('Y-m-d');
            $params['status'] = 1;
            $params['state'] = 1;
            $params['ip'] = request()->ip();
            $params['city'] = getLocationByIp($params['ip']);
            if(empty($params['weburl'])) {
                $params['weburl'] = '后台添加';
            }

            $this->insert($params);
        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '添加管理员成功');
    }


    /**
     * 获取管理员信息
     * @param $orderId
     * @return array
     */
    public function getOrderById($orderId)
    {
        try {

            $info = $this->where('id', $orderId)->findOrEmpty()->toArray();
        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $info, 'ok');
    }

    /**
     * 编辑订单
     * @param $order
     * @return array
     */
    public function editOrder($order)
    {
        try {

            $has = $this->where('name', $order['name'])->where('id', '<>', $order['id'])
                ->findOrEmpty()->toArray();
            if(!empty($has)) {
                return modelReMsg(-2, '', '订单已经存在');
            }
            $order['update_time'] = date('Y-m-d H:i:s');

            $this->save($order, ['id' => $order['id']]);
        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '编辑订单成功');
    }

    /**
     * 删除订单
     * @param $orderId
     * @return array
     */
    public function delOrder($orderId)
    {
        try {

            $this->where('id', $orderId)->delete();
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '删除成功');
    }

    /**
     * 获取订单信息 whereTime
     * @param $params
     * @return array
     */
    public function getOrderDays($limit, $whereTime) {
        try {
            $res = $this->whereTime('add_time', $whereTime)
                ->where('status',1)
                ->order('add_time', 'desc')
                ->paginate($limit);

        }catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

}